package com.zushiye.order.controller;


import com.zushiye.order.model.CartItemBean;
import com.zushiye.order.model.request.CartDelRequest;
import com.zushiye.order.model.request.CartQueryRequest;
import com.zushiye.order.model.response.BaseResponse;
import com.zushiye.order.model.response.CartListResponse;
import com.zushiye.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService service;

    //http://127.0.0.1:8080/cart/add
    @PostMapping(value = "/add")
    public BaseResponse add(@RequestBody CartItemBean itemBean) {
        BaseResponse response = new BaseResponse();
        if (itemBean.user_id == 0) {
            response.msg = "添加失败";
            return response;
        }
        int i = service.insert(itemBean);
        if (i >= 0) {
            response.msg = "添加成功";
            response.code = 1;
        } else {
            response.msg = "添加失败";
        }
        return response;
    }

    @PostMapping(value = "/delete")
    public BaseResponse delete(@RequestBody CartDelRequest req) {
        BaseResponse response = new BaseResponse();
        int i = service.delete(req.id);
        if (i >= 0) {
            response.msg = "删除成功";
            response.code = 1;
        } else {
            response.msg = "删除失败";
        }
        return response;
    }

    /*
     * ids 必须带空格 (24, 25)
     */
    @PostMapping(value = "/batchDelete")
    public BaseResponse batchDelete(@RequestBody CartDelRequest req) {
        BaseResponse response = new BaseResponse();
        int i = service.batchDelete(req.idList);
        if (i >= 0) {
            response.msg = "批量删除成功";
            response.code = 1;
        } else {
            response.msg = "批量删除失败";
        }
        return response;
    }

    @PostMapping(value = "/queryAll")
    public CartListResponse queryAll(@RequestBody CartQueryRequest req) {
        CartListResponse response = new CartListResponse();
        List<CartItemBean> list = service.queryAll(req.user_id);
        if (list == null) {
            response.msg = "查询失败";
        } else {
            response.list = list;
            response.msg = "查询成功";
            response.code = 1;
        }
        return response;
    }

}
